package cn.mandifal.manager.service.impl;

import cn.mandifal.manager.service.GoodsManagementService;
import cn.mandifal.mapper.DpGoodsDescMapper;
import cn.mandifal.mapper.DpGoodsMapper;
import cn.mandifal.mapper.DpItemMapper;
import cn.mandifal.pojo.*;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * 平台商品管理服务实现层
 * @author shun
 */
@Service
@Transactional
public class GoodsManagementServiceImpl implements GoodsManagementService {

    @Autowired
    private DpGoodsMapper dpGoodsMapper;
    @Autowired
    private DpItemMapper dpItemMapper;
    @Autowired
    private DpGoodsDescMapper dpGoodsDescMapper;

    /**
     * 查询所有
     * @return 返回结果
     */
    @Override
    public List<DpGoods> findAll() {
        return dpGoodsMapper.selectByExample(null);
    }

    /**
     * 模糊查询，无条件时查询所有
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param dpGoods 商品 spu 实体
     * @return 返回分页结果
     */
    @Override
    public PageResult search(int pageNum, int pageSize, DpGoods dpGoods) {
        PageHelper.startPage(pageNum,pageSize);
        DpGoodsExample dpGoodsExample= new DpGoodsExample();
        DpGoodsExample.Criteria criteria = dpGoodsExample.createCriteria();
        //设置逻辑删除的不可见
        criteria.andIsDeleteIsNull();
        //判断商品状态
        if(dpGoods.getAuditStatus() !=null && dpGoods.getAuditStatus().length()>0){
            criteria.andAuditStatusEqualTo(dpGoods.getAuditStatus());
        }
        //判断商品名字
        if (dpGoods.getGoodsName() !=null && dpGoods.getGoodsName().length()>0){
            criteria.andGoodsNameLike("%"+dpGoods.getGoodsName()+"%");
        }
        //两个 if 都为 false 时，查询所有
        Page<DpGoods>page= (Page<DpGoods>) dpGoodsMapper.selectByExample(dpGoodsExample);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询一个商品信息
     * @param id 商品 id
     * @return 返回查询结果
     */
    @Override
    public GoodsCombine findOne(Long id) {
        GoodsCombine goodsCombine = new GoodsCombine();
        //设置商品 spu
        goodsCombine.setDpGoods(dpGoodsMapper.selectByPrimaryKey(id));
        //设置商品详情
        goodsCombine.setDpGoodsDesc(dpGoodsDescMapper.selectByPrimaryKey(id));
        //查询当前 spu 对应的 sku
        DpItemExample dpItemExample=new DpItemExample();
        DpItemExample.Criteria criteria = dpItemExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        goodsCombine.setDpItemList(dpItemMapper.selectByExample(dpItemExample));
        return goodsCombine;
    }

    /**
     * 更新商品状态
     * @param ids 待修改的商品 id
     * @param status 状态
     */
    @Override
    public void updateAuditStatus(Long[] ids, String status) {
        for (Long id:ids){
            DpGoods dpGoods = dpGoodsMapper.selectByPrimaryKey(id);
            //通过 spuid 查找sku
            DpItemExample dpItemExample=new DpItemExample();
            DpItemExample.Criteria criteria = dpItemExample.createCriteria();
            criteria.andGoodsIdEqualTo(id);
            dpGoods.setAuditStatus(status);
            //如果状态为 2 审核通过 则设置默认上架商品spu
            if ("2".equals(status)){
                //为 1 上架 ，2 为下架
                dpGoods.setIsMarketable("1");
            }
            dpGoodsMapper.updateByPrimaryKey(dpGoods);
        }
    }

    /**
     * 逻辑删除
     * @param ids 商品 spu id
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id:ids){
            DpGoods dpGoods = dpGoodsMapper.selectByPrimaryKey(id);
            //如果商品通过审核设置下架状态
            if ("2".equals(dpGoods.getAuditStatus())){
                dpGoods.setIsMarketable("2");
            }
            dpGoods.setIsDelete("1");
            dpGoodsMapper.updateByPrimaryKey(dpGoods);
            //逻辑删除此 spu 下的所有 sku
            DpItemExample dpItemExample=new DpItemExample();
            DpItemExample.Criteria criteria = dpItemExample.createCriteria();
            criteria.andGoodsIdEqualTo(id);
            //得到此 spu 下的所有 sku
            List<DpItem> dpItems = dpItemMapper.selectByExample(dpItemExample);
            for (DpItem dpItem:dpItems) {
                //3为删除状态
                dpItem.setStatus("3");
                dpItemMapper.updateByPrimaryKey(dpItem);
            }
        }
    }

    /**
     * 查询，通过 spuid 和装填
     * @param goodsIds spu id
     * @param status 装填
     * @return 返回查到的列表
     */
    @Override
    public List<DpItem> findItemListByGoodsIdAndStatus(Long[] goodsIds, String status) {
        DpItemExample example=new DpItemExample();
        DpItemExample.Criteria criteria = example.createCriteria();
        //相当于 sql 语句中的 in，组查询
        criteria.andGoodsIdIn(Arrays.asList(goodsIds));
        criteria.andStatusEqualTo(status);
        List<DpItem> dpItems = dpItemMapper.selectByExample(example);
        System.out.println("查到的商品 sku 为："+dpItems+"状态为："+status);
        return dpItems;
    }

}
